<?php

namespace App\Service;

use App\Exceptions\ApiException;
use GuzzleHttp\Client;

class SendSms
{
    protected $client;
    public function __construct(Client  $client)
    {
        $this->client=$client;
    }

    public function sendSms($mobile,$code){
      $respone=  $this->client->post('http://v.juhe.cn/sms/send',[
            'headers'=>[
                'Content-Type'=>'application/x-www-form-urlencoded'
            ],
            'form_params'=>[
                'mobile'=>$mobile,
                'key'=>'273f64a2df55c0d297f57e79e441cfd5',
                'tpl_id'=>260828,
                'vars'=>json_encode(['code'=>$code,'m'=>5])
            ]
        ]);
      $json= json_decode($respone->getBody()->getContents(),true);
      if (isset($json['error_code'])&&$json['error_code']==0){
          return true;
      }
      throw new ApiException($json['reason']);
    }

}
